﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

/// <summary>
/// Launcher の概要の説明です
/// </summary>
public class Launcher
{
    private static bool UpdateMemberId(string group, out int id)
    {
        id = 0;
        if (!RegisterIfNotExist(group)) return false;
        if (!IncrementMember(group,out id)) return false;
        return true;

    }
    private static string GetConnectionString()
    {
        ConnectionStringSettings setting = ConfigurationManager.ConnectionStrings["bcConnectionString1"];
        return setting.ConnectionString;
    }
    private static bool RegisterIfNotExist(string group)
    {
        SqlConnection objDb = new SqlConnection(GetConnectionString());
        SqlCommand objCom = new SqlCommand("SELECT count(*) FROM usergrouptable WHERE usergroup=@group", objDb);
        objCom.Parameters.AddWithValue("@group", Sanitizing.SafeSQLLiteral(group));

        int cnt = 0;
        try
        {
            objDb.Open();
            cnt = (int)objCom.ExecuteScalar();
        }
        finally
        {
            objDb.Close();
        }

        if (cnt == 1) return true;

        //
        SqlConnection objDb2 = new SqlConnection(GetConnectionString());
        SqlCommand objCom2 = new SqlCommand("INSERT INTO usergrouptable (usergroup,closed,nmember) VALUES (@group,'0','0')", objDb2);
        objCom2.Parameters.AddWithValue("@group", Sanitizing.SafeSQLLiteral(group));
        int affectedRows = 0;
        try
        {
            objDb2.Open();
            affectedRows = objCom2.ExecuteNonQuery();
        }
        finally
        {
            objDb2.Close();
        }

        if (affectedRows != 1) return false;

        return true;
    }
    public static bool GetNumberOfGroup(string group,out int cnt)
    {
        SqlConnection objDb = new SqlConnection(GetConnectionString());
        SqlCommand objCom = new SqlCommand("SELECT nmember FROM usergrouptable WHERE usergroup=@group", objDb);
        objCom.Parameters.AddWithValue("@group", Sanitizing.SafeSQLLiteral(group));

        cnt = -1;
        try
        {
            objDb.Open();
            cnt = (int)objCom.ExecuteScalar();
        }
        finally
        {
            objDb.Close();
        }

        if (cnt < 0)
        {
            cnt = 0;
            return false;
        }
        return true;
    }
    private static bool IncrementMember(string group, out int cnt)
    {
        if (!GetNumberOfGroup(group, out cnt)) return false;

        cnt++;

        SqlConnection objDb2 = new SqlConnection(GetConnectionString());
        SqlCommand objCom2 = new SqlCommand("UPDATE usergrouptable SET nmember = @nmember WHERE usergroup=@group", objDb2);
        objCom2.Parameters.AddWithValue("@nmember", cnt);
        objCom2.Parameters.AddWithValue("@group", Sanitizing.SafeSQLLiteral(group));
        int affectedRows = 0;
        try
        {
            objDb2.Open();
            affectedRows = objCom2.ExecuteNonQuery();
        }
        finally
        {
            objDb2.Close();
        }

        if (affectedRows != 1) return false;

        return true;

    }


    //////////////////////////////////////////////////////////

    private static string RequireSendLogString()
    {
        bool b;
        string s = ConfigurationManager.AppSettings["RequireSendLog"];
        if (bool.TryParse(s, out b))
        {
            if (b)
            {
                return "&sl=true";
            }
        }
        return "";
    }
    private static string UpdateAndGetName(string group)
    {
        string name = group;
        int id;
        if (UpdateMemberId(group, out id))
        {
            name += "_" + id.ToString();
        }
        else
        {
            string tim = DateTime.Now.Ticks.ToString();
            tim = tim.Substring(0, tim.Length - 4);
            name += "_" + tim;
        }
        return name;
    }
	public static string LaunchEasyUrl(string group)
	{ //easystarter用
        string url = ConfigurationManager.AppSettings["StudentExeUrl"];
        //return url + @"?sync=true&ccas=true&rpi=false&sii=true&spi=false&stai=true&sv=false&group=" + group + @"&name=" + name;
        return url + @"?sync=true&role=easy&group=" + group + @"&name=" + UpdateAndGetName(group) + RequireSendLogString();

    }

    public static string LaunchStudentUrl(string group)
    {
        string url = ConfigurationManager.AppSettings["StudentExeUrl"];
        //return url + @"?sync=true&ccas=true&rpi=false&sii=true&spi=false&stai=true&sv=false&group=" + group + @"&name=" + name;
        return url + @"?sync=true&role=student&group=" + group + @"&name=" + UpdateAndGetName(group) + RequireSendLogString();
    }

    public static string LaunchAudienceUrl(string group)
    {
        string url = ConfigurationManager.AppSettings["StudentExeUrl"];
        //return url + @"?sync=true&ccas=true&rpi=false&sii=true&spi=false&stai=true&sv=false&group=" + group + @"&name=" + name;
        return url + @"?sync=true&role=audience&group=" + group + @"&name=" + UpdateAndGetName(group) + RequireSendLogString();
    }

    public static string LaunchDiscussantUrl(string group)
    {
        string url = ConfigurationManager.AppSettings["StudentExeUrl"];
        //return url + @"?sync=true&ccas=true&rpi=false&sii=true&spi=false&stai=true&sv=false&group=" + group + @"&name=" + name;
        return url + @"?sync=true&role=discussant&group=" + group + @"&name=" + UpdateAndGetName(group) + RequireSendLogString();
    }

    public static string LaunchPresenterUrl(string group)
    {
        string url = ConfigurationManager.AppSettings["StudentExeUrl"];
        //return url + @"?sync=true&ccas=true&rpi=false&sii=true&spi=false&stai=true&sv=false&group=" + group + @"&name=" + name;
        return url + @"?sync=true&role=presenter&group=" + group + @"&name=" + UpdateAndGetName(group) + RequireSendLogString();
    }

    public static string LaunchManagerUrl(string group)
    {
        string url = ConfigurationManager.AppSettings["StudentExeUrl"];
        //return url + @"?sync=true&ccas=true&rpi=false&sii=true&spi=false&stai=true&sv=false&group=" + group + @"&name=" + name;
        return url + @"?sync=true&role=manager&group=" + group + @"&name=" + UpdateAndGetName(group) + RequireSendLogString();
    }

    public static string LaunchOverviewUrl(string group)
    {
        string url = ConfigurationManager.AppSettings["StudentExeUrl"];
        //return url + @"?sync=true&ccas=true&rpi=false&sii=true&spi=false&stai=true&sv=false&group=" + group + @"&name=" + name;
        return url + @"?sync=true&role=overview&group=" + group + @"&name=" + UpdateAndGetName(group) + RequireSendLogString();
    }

    public static string LaunchAdjacentViewUrl(string group,int offset)
    {
        string url = ConfigurationManager.AppSettings["StudentExeUrl"];
        //return url + @"?sync=true&ccas=true&rpi=false&sii=true&spi=false&stai=true&sv=false&group=" + group + @"&name=" + name;
        return url + @"?sync=true&role=adjacent" + offset.ToString() + @"&group=" + group + @"&name=" + UpdateAndGetName(group) + RequireSendLogString();
    }

    //public static string LaunchQuizMasterUrl(string group)
    //{
    //    string url = ConfigurationManager.AppSettings["StudentExeUrl"];
    //    //return url + @"?sync=true&ccas=true&rpi=false&sii=true&spi=false&stai=true&sv=false&group=" + group + @"&name=" + name;
    //    return url + @"?sync=true&role=adjacent" + offset.ToString() + @"&group=" + group + @"&name=" + UpdateAndGetName(group) + RequireSendLogString();
    //}
}
